<?php
// @formatter:off
/**
 * @file t.group.create.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-22 17:58:11 +0000 (Sat, 22 Jun 2013) $
 *
 * $Id: t.group.create.php 83 2013-06-22 17:58:11Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.2/tools/t.group.create.php $
 */

$wcToolScripts["group.create"]	= array(
	"name"		=> "Create Group",
	"class"		=> "WCToolGroupCreate",
	"url"		=> "http://code.google.com/p/wcomix/",
	"enabled"	=> true
);
// @formatter:on

class WCToolGroupCreate extends WCTool {
	public function run($spacer = "") {
		global $wcTagsHolder;
		global $wcSpecialTags;
		global $wcGroupsHolder;
		global $wcDefaults;
		global $wcDatabaseAccess;
		global $wcOptions;
		//
		// Checking that Read Only Mode is not activated.
		if(!$wcDefaults["read-only"]) {
			if($wcOptions->activated("Name") && $wcOptions->activated("Path")) {
				$ok = true;

				$name = $wcOptions->value("Name");
				$path = $wcOptions->value("Path");
				$trash = $wcOptions->activated("Trashcan") ? $wcOptions->value("Trashcan") : false;
				$public = $wcOptions->activated("Public");

				//
				// Cleaning paths.
				while(strpos($path, '//')) {
					$path = str_replace('//', '/', $path);
				}
				if($path[strlen($path) - 1] == '/') {
					$path = substr($path, 0, strlen($path) - 1);
				}
				while(strpos($trash, '//')) {
					$trash = str_replace('//', '/', $trash);
				}
				if($trash[strlen($trash) - 1] == '/') {
					$trash = substr($trash, 0, strlen($trash) - 1);
				}

				//
				// Checking name availability.
				$group = $wcGroupsHolder->groupByName($name);
				if($ok && $group) {
					$ok = false;
					echo $spacer.WCColorText::ShellRed("Failed").": Group '{$name}' already exists.\n";
				}

				//
				// Checking if paths are absolute.
				if($ok) {
					if(!$path || $path[0] != "/") {
						$ok = false;
						echo $spacer.WCColorText::ShellRed("Failed").": Directory path '{$path}' is not absolute.\n";
					}
					if($trash && $trash[0] != "/") {
						$ok = false;
						echo $spacer.WCColorText::ShellRed("Failed").": Trashcan path '{$trash}' is not absolute.\n";
					}
				}

				//
				// Checking if paths are existing directories.
				if($ok) {
					if(!is_dir($path)) {
						$ok = false;
						echo $spacer.WCColorText::ShellRed("Failed").": Path '{$path}' is not a directory.\n";
					}
					if($trash && !is_dir($path.$trash)) {
						$ok = false;
						echo $spacer.WCColorText::ShellRed("Failed").": Path '{$path}{$trash}' for trashcan is not a directory.\n";
					}
				}

				//
				// Creating new group
				if($ok) {
					$ok = false;

					$stmtId = __CLASS__."::".__FUNCTION__;
					$stmtGId = "{$stmtId}[group]";

					$query = "insert\n";
					$query .= "        into {$this->_dbprefix}groups (\n";
					$query .= "                grp_name, grp_root, grp_public, grp_trash)\n";
					$query .= "        values (:name, :root, :public, :trash)\n";
					$stmtG = $this->_db->prepare($stmtGId, $query);

					$params = array(
						":name" => $name,
						":root" => $path,
						":public" => $public ? "Y" : "N",
						":trash" => $trash
					);

					echo "{$spacer}Creating group '{$name}':\n";
					if($stmtG->execute($params)) {
						$wcGroupsHolder->reload();
						$group = $wcGroupsHolder->groupById($this->_db->lastInsertId());
						if($group) {
							$stmtDId = "{$stmtId}[directory]";

							$query = "insert\n";
							$query .= "        into {$this->_dbprefix}directories (\n";
							$query .= "                dir_group, dir_name)\n";
							$query .= "        values (:group, :name)\n";
							$stmtD = $this->_db->prepare($stmtDId, $query);

							$params = array();
							$params[":group"] = $group->id;

							$params[":name"] = "/";
							echo "{$spacer}\tAdding directory '{$params[":name"]}' to group '{$group->name}' ({$group->id}):\n";
							$stmtD->execute($params);

							if($trash) {
								$params[":name"] = $trash;
								echo "{$spacer}\tAdding directory '{$params[":name"]}' to group '{$group->name}' ({$group->id}):\n";
								$stmtD->execute($params);
							}
						} else {
							echo "{$spacer}\t".WCColorText::ShellRed("Failed").": Newly created group can't be loaded.\n";
						}
					} else {
						echo "{$spacer}\t".WCColorText::ShellRed("Failed").": Unable to create group due to a database error.\n";
					}
				}
			} elseif(!$wcOptions->activated("Name")) {
				echo $spacer.WCColorText::ShellRed("Failed").": No group name specified.\n";
			} elseif(!$wcOptions->activated("Path")) {
				echo $spacer.WCColorText::ShellRed("Failed").": No directory path specified.\n";
			} else {
				$this->showHelp($spacer);
			}
		} else {
			echo $spacer.WCColorText::ShellYellow("ReadOnlyMode activated.\n");
		}
	}
	public function showHelp($spacer = "") {
		echo "{$spacer}Options:\n";
		echo "{$spacer}\t-n name, --name name\n";
		echo "{$spacer}\t\tName to asign to the new group.\n\n";
		echo "{$spacer}\t-p directory-path, --path directory-path\n";
		echo "{$spacer}\t\tNew group root directory.\n\n";
		echo "{$spacer}\t-t subdirectory-path, --trashcan subdirectory-path\n";
		echo "{$spacer}\t\tSubdirectory where to drop garbage. It must be something like:.\n";
		echo "{$spacer}\t\t\t/<name>\n\n";
		echo "{$spacer}\t-P, --public\n";
		echo "{$spacer}\t\tThis flag indicates if the group must be created as a public group.\n";
	}
	protected function loadOptions() {
		if(!$this->_optionsLoaded) {
			global $wcOptions;

			if($wcOptions->addValueOption("Name")) {
				$wcOptions->addOptionTrigger("Name", "n");
				$wcOptions->addOptionTrigger("Name", "name");
			}
			if($wcOptions->addValueOption("Path")) {
				$wcOptions->addOptionTrigger("Path", "p");
				$wcOptions->addOptionTrigger("Path", "path");
			}
			if($wcOptions->addValueOption("Trashcan")) {
				$wcOptions->addOptionTrigger("Trashcan", "t");
				$wcOptions->addOptionTrigger("Trashcan", "trashcan");
			}
			if($wcOptions->addOption("Public")) {
				$wcOptions->addOptionTrigger("Public", "P");
				$wcOptions->addOptionTrigger("Public", "public");
			}

			$wcOptions->check();

			$this->_optionsLoaded = true;
		}
	}
}
?>